<template>
  <view>
    <view class="goods-list">
      <view @click="gotoDetail(goods)" v-for="(goods, i) in goodsList" :key="i">
       <my-goods :goods="goods"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj:{
          // 查询关键词
           query: '',
          // 商品分类Id
           cid: '',
           // 页码值
          pagenum: 1,
          // 每页显示多少条数据
          pagesize: 10
        },
        // 数据总数
        total:0,
        goodsList:[],
        // 节流阀
        isloading:false
      };
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      this.getGoodsList()
    },
    methods:{
      // 获取商品列表数据
     async getGoodsList(cb){
       this.isloading = true
    const {data:res} = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
      this.isloading = false
      cb && cb()
      if(res.meta.status !== 200) return uni.$showMsg()
      this.goodsList = [...this.goodsList,...res.message.goods]
      this.total = res.message.total
      },
      // 点击商品跳转到商品详情
      gotoDetail(goods){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
        })
      }
    },
    // 上拉加载更多
    onReachBottom() {
      // 判断是否还有下一页数据
      if(this.queryObj.pagesize * this.queryObj.pagenum >= this.total) return uni.$showMsg('没有更多数据了')
      
      if(this.isloading) return false
      // 页码自增+1 发请求
      this.queryObj.pagenum++
      this.getGoodsList()
    },
    // 下拉刷新的事件
    onPullDownRefresh() {
      // 重置关键数据
      this.queryObj.pagenum = 1
      this.total = 0
      this.isloading = false
      this.goodsList = []
      // 重新发请求
      this.getGoodsList(()=>uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">

</style>
